n = int(input())
N = 30
a = [[0] * N for _ in range(N)]
st = [False] * N


def dfs(id, ra):
    num = 0
    for i in range(n):
        if id == i or a[id][i] == 0:
            continue
        if ra >= a[id][i]:
            if st[i]:
                continue
            st[i] = True
            num += dfs(i, a[id][i])
            st[i] = False
    return num + 1


for i in range(n):
    a[i] = list(map(int, input().split()))

x = int(input())
res, ans = -1, 0
while True:
    if x == -1:
        break
    for i in range(n):
        if x == i:
            continue
        st[x] = True
        t = dfs(x, 10)
        st[x] = False
        if t > ans:
            ans = t
            res = x
    x = int(input())

print(res)
